<?php
class User_model extends CI_Model {
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
	public function find_by_id($id) {
		$this->db->where('id', $id);
		return $this->db->get('users')->row_array();
	}
	
	/**
	 * 返回用户数组
	 * @param array $ids
	 */
	public function find_by_ids ($ids) {
		$sql = " select id, email, nickname from users where id in(" . implode(",", $ids) . ")" ;
		$result = $this->db->query($sql)->result_array();
		$return_array = array();
		for ($i = 0, $n = count($result); $i < $n; $i++) {
			$return_array[$result[$i]['id']] = $result[$i];
		}
		return $return_array;
	}
    /**
	 * 获取用户信息
	 * @param string $email
	 */
	public function find_user_by_email($email) {
		$this->db->where('email', $email);
		return $this->db->get('users')->row_array();
	}
	
 	/**
	 * 获取用户信息
	 * @param string $nick
	 */
	public function find_user_by_nick($nick) {
		$this->db->where('nickname', $nick);
		return $this->db->get('users')->row_array();
	}
	/**
	 * 插入用户数据
	 * @param array $data
	 */
	public function add($data) {
		$salt = chr(rand(65,90)) . rand(0, 9);
		$data['password'] = md5($data['password'] . $salt) . ":" . $salt;
		$data['created'] = date("Y-m-d H:i:s");
		$this->db->insert("users", $data);
		return $this->db->insert_id();
	}
	
	/**
	 * 检查用户是否存在
	 * @param string $email
	 */
	public function is_user_exist($email, $nickname) {
		$this->db->where('email', $email);
		$this->db->or_where('nickname', $nickname);
		return $this->db->get('users')->num_rows();
	}
	
	public function is_user_active($id) {
		$this->db->select("status");
		$this->db->where('id', $id);
		$row = $this->db->get("users")->row_array();
		return empty($row['status']) ? '0' : $row['status'];
	}
	
	public function is_user_forbidden_talk($id) {
		$this->db->select("status");
		$this->db->where('id', $id);
		$row = $this->db->get("users")->row_array();
		return $row['status'] == -2 ? 1 : 0;
	}
	/**
	 * 用户激活
	 * @param int $user_id
	 * @param string $active_code
	 */
	public function active($user_id, $active_code, $data) {
		$sql = "select count(*) from users where id=" . $user_id . " and active_code=" . $this->db->escape($active_code);
		if ($this->db->query($sql)->num_rows() > 0) {
			$data['status'] = 1;
			$data['active_time'] = date("Y-m-d H:i:s");
			$this->db->where('id', $user_id);
			$this->db->update('users', $data); 
			return 1;
		} else {
			return 0;
		}
	}
	
	public function change_password($id, $password) {
		$salt = chr(rand(65,90)) . rand(0, 9);
		$encryt_password = md5($password . $salt) . ":" . $salt;
		$sql = " update users set password = " . $this->db->escape($encryt_password) . " where id = ". intval($id);
		$this->db->query($sql);
	}
	/**
	 * 用户发布的story
	 * @param int $user_id
	 */
	public function get_publish_story_count($user_id) {
		$this->db->where('created_by', intval($user_id));
		$this->db->from('emergencies');
		return $this->db->count_all_results();
	}
	/**
	 * 用户申请的story数量
	 * @param int $user_id
	 */
	public function get_applied_stroy_count($user_id) {
		$sql = " select count(distinct(emergency_id)) as total from  user_emergency  where user_id=" . $user_id;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	
	/**
	 * 用户参与group数量
	 * @param int $user_id
	 */
	public function get_joined_group_count($user_id) {
		$sql = " select count(distinct(group_id)) as total from  user_group  where user_id=" . $user_id;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	/**
	 * 获取用户的行家
	 * @param int $user_id
	 */
	public function find_experts_by_user_id($user_id) {
		$this->db->select(array('id', 'title'));
		$this->db->where('created_by', $user_id);
		$result = $this->db->get("experts")->result_array();
		return $result;
	}
	/**
	 * 获取用户的avatar信息
	 * @param int $user_id
	 */
	public function find_avatar_by_user_id($user_id) {
		$this->db->select(array('id', 'avatar', 'thumb_avatar'));
		$this->db->where('id', $user_id);
		$result = $this->db->get("users")->row_array();
		return $result;
	}
	/**
	 * 修改用户avatar信息
	 * @param int $user_id
	 * @param string $avatar_file_name
	 */
	public function update_avatar($user_id, $avatar_array) {
		$sql = " update users set avatar=" .$this->db->escape($avatar_array['file_name']) .
		" ,thumb_avatar=" . $this->db->escape($avatar_array['raw_name'] . "_thumb" . $avatar_array['file_ext'])
		 . " where id=" . $user_id;
		return $this->db->query($sql);
	}
	/**
	 * 获取用户认证信息
	 * @param int $user_id
	 */
	public function find_vertification_by_user_id($user_id) {
		$this->db->select(array('obj_name'));
		$this->db->where('status', 1);
		$this->db->where('created_by', $user_id);
		$result = $this->db->get("vertifications")->result_array();
		return $result;
	}
	/**
	 * 获取用户联系信息
	 * @param int $user_id
	 */
	public function find_contact_by_user_id($user_id) {
		$this->db->where('user_id', $user_id);
		$result = $this->db->get("contacts")->row_array();
		return $result;
	}
	/**
	 * 注销用户
	 * @param int $user_id
	 */
	public function set_user_noactive($user_id) {
		$sql = "update users set status=-1 where id=" . intval($user_id);
		return $this->db->query($sql);
	}
	
	public function find_user_address($user_id) {
		$sql = " select p.province,c.city,a.area from  users u 
			left join province p on u.province_id = p.provinceID
			left join city c on u.city_id = c.cityID
			left join area a on u.area_id = a.areaID
		 	where u.id=" . intval($user_id);
		$result = $this->db->query($sql)->row_array();
		$address = "";
		if (!empty($result['province'])) {
			$address .= $result['province'];
		}
		if (!empty($result['city']) && $result['city'] != '市辖区' && $result['city'] != '县') {
			$address .= $result['city'];
		}
		if (!empty($result['area'])) {
			$address .= $result['area'];
		}
		
		if (empty($address)) {
			$address = "--";
		}
		return $address;
	}
	
	public function is_vertification($user_id) {
		$sql = " select is_vertification from users 
		 	where id=" . intval($user_id);
		$result = $this->db->query($sql)->row_array();
		return empty($result['is_vertification']) ? 0 : $result['is_vertification'];
	}
	
	public function is_expert_vertification($user_id) {
		$sql = " select count(*) as total from  vertifications  where status= 1 and type=2 and created_by=" . $user_id;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	
	public function find_employ_count($user_ids) {
		$sql = " select count(*) as total,user_id from  user_emergency  where status= 1 and user_id in(" . $user_ids . ")";
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	
	public function find_vertification_info($user_ids) {
		$sql = " select count(*) as total ,created_by from  vertifications  where status= 1 and obj_name='身份证' and created_by in(" . $user_ids . ") group by created_by";
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
	
	public function find_subscribes($user_id) {
		$sql = " select p.province as province_name,s.*,c.city as city_name,a.area as area_name, ct.name from subscribes s
		left join province p on s.province=p.provinceID
		left join city c on s.city=c.cityID
		left join area a on s.area = a.areaID
		left join categories ct on s.category_id=ct.id
		where s.created_by=" . $user_id . "  order by s.created desc ";
		$result =  $this->db->query($sql)->result_array();
		for ($i = 0, $n = count($result); $i < $n; $i++) {
			$result[$i]['title'] = '';
			//$result[$i]['title'] = empty($result[$i]['province_name']) ? '' : $result[$i]['province_name'];
			//$result[$i]['title'] .= empty($result[$i]['city_name']) || $result[$i]['city_name'] == '辖区' || $result[$i]['city_name'] == '县'? '' : $result[$i]['city_name']; 
			$result[$i]['title'] .= empty($result[$i]['area_name']) ? '' : $result[$i]['area_name'];
			$result[$i]['title'] .= empty($result[$i]['name']) ? '' : $result[$i]['name'];
			
			$story_count_sql = " select count(*) as total from emergencies where 1=1 "
			. (!empty($result[$i]['category_id']) ? " and category_id=" . $result[$i]['category_id'] : '')
			. (!empty($result[$i]['province']) ? " and province_id=" . $result[$i]['province'] : '')
			. (!empty($result[$i]['city']) ? " and city_id =" . $result[$i]['city']: '')
			. (!empty($result[$i]['area']) ? " and area_id =" . $result[$i]['area'] : '');
			$result_count = $this->db->query($story_count_sql)->row_array();
			$result[$i]['story_count'] = $result_count['total'];
			if (empty($result[$i]['title'])) {
				unset($result[$i]);
			}
			
								
		}
		return $result;
	}
	
	public function update_code($code, $user_id) {
		$data['active_code'] = $code;
		$this->db->where('id', $user_id);
		$this->db->update('users', $data); 
	}
	public function update_user_status($user_id, $status) {
		$data['status'] = $status;
		$this->db->where('id', $user_id);
		$this->db->update('users', $data); 
	}
}